/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.Warehouse;
import POJO.Warehousedetail;
import Util.DataAccessUtil;
import com.sun.corba.se.impl.oa.toa.TOA;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author roseinlove09
 */
public class WarehouseDAO {
    public static List<Warehouse> getListWarehouses() throws Exception{
        String hql = "SELECT p from Warehouse p "
                     +"LEFT JOIN FETCH p.store "
                     +"LEFT JOIN FETCH p.employee";       
        
        return DataAccessUtil.getList(hql);
    }
    public static List<Warehousedetail> getListWarehouseDetails(String warehouseId) throws Exception{
        String hql = "SELECT d from Warehousedetail d "
                      +"LEFT JOIN FETCH d.product p "
                      +"LEFT JOIN FETCH p.productcat c "
                      +"WHERE d.warehouse.id = :warehouseId";       
        Map<String,Object> params = new HashMap<>();
        params.put("warehouseId", warehouseId);
        return DataAccessUtil.getList(hql,params);
    }
    
    public static boolean checkExistsWarehouse(String code) throws Exception{
        String hql = "SELECT w FROM Warehouse w WHERE w.id = :code";
        Map<String,Object> params = new HashMap<>();
        params.put("code", code);
        return DataAccessUtil.getList(hql,params).size() > 0;
    }
    public static List<Warehouse> searchWarehouses(Map<String,Object> whCdtions) throws Exception{
        String id = whCdtions.get("id").toString();
        String employeeName = whCdtions.get("employeeName").toString();
        Date dateFrom =  (Date) whCdtions.get("dateForm");
        Date dateTo =  (Date) whCdtions.get("dateTo");
        int productCatId =  (int) whCdtions.get("productCatId");
        String productId =  (String) whCdtions.get("productId");
        
        String hql = "SELECT DISTINCT w FROM Warehouse w "
                    +"LEFT JOIN FETCH w.employee e "
                    +"LEFT JOIN FETCH w.warehousedetails d "
                    +"LEFT JOIN FETCH d.product p "
                    +"LEFT JOIN FETCH p.productcat c "  
                    +"WHERE 1 = 1 ";
        Map<String,Object> params = new HashMap<>();
        
        if(!id.isEmpty()){
            hql += "AND str(w.id) like :idWh ";
            params.put("idWh", "%"+id+"%");
        }
        if(!employeeName.isEmpty()){
            hql += "AND lower(e.name) like :employeeName ";
            params.put("employeeName", "%"+employeeName.toLowerCase()+"%");
        }
        if(productCatId > 0){
            hql += "AND c.id = :productCatId ";
            params.put("productCatId", productCatId);
            if(!productId.isEmpty()){
                hql += "AND p.id = :productId ";
                params.put("productId", productId);
            }
        }
        
        hql += "AND w.warehousedate >= :dateFrom AND w.warehousedate <= :dateTo ";
        params.put("dateFrom", dateFrom);
        params.put("dateTo", dateTo);  
        
        return DataAccessUtil.getList(hql, params);
    }  
    
}
